package com.administrator.scoreManageSys.controller.system;

import com.administrator.scoreManageSys.service.RoleManageService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 角色管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/system/roleManage")
public class RoleManageController {

    @Resource
    private RoleManageService roleManageService;

    /**
     * 获取角色列表
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/roleList")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:list", logical = Logical.OR)
    public JSONObject roleList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            return roleManageService.roleList(pageNum,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取角色列表 异常");
        }
    }


    /**
     * 添加角色
     *
     * @return 添加菜单结果
     */
    @PostMapping(value = "/roleAdd")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:add", logical = Logical.OR)
    public JSONObject roleAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            String name = data.getString("name");
            return roleManageService.roleAdd(name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加角色 异常");
        }
    }

    /**
     * 修改角色
     *
     * @return 修改菜单
     */
    @PostMapping(value = "/roleUpdate")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:edit", logical = Logical.OR)
    public JSONObject roleUpdate(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            String name = data.getString("name");
            Long id = data.getLong("id");
            return roleManageService.roleUpdate(id,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改角色 异常");
        }
    }

    /**
     * 删除角色
     *
     * @return 删除菜单
     */
    @PostMapping(value = "/roleDelete")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:list", logical = Logical.OR)
    public JSONObject roleDelete(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            Long id = data.getLong("id");
            return roleManageService.roleDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除角色 异常");
        }
    }

    /**
     * 查看该角色包含人员
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/roleUserList")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:list", logical = Logical.OR)
    public JSONObject roleUserList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Long id = data.getLong("id");
            return roleManageService.roleUserList(id,pageNum,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查看该角色包含人员 异常");
        }
    }

    /**
     * 查看该角色权限列表
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/rolePermissionList")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:list", logical = Logical.OR)
    public JSONObject rolePermissionList(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return roleManageService.rolePermissionList(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查看该角色权限列表 异常");
        }
    }

    /**
     * 更新当前角色的权限菜单
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/rolePermissionUpdate")
    @RequiresRoles("authority")
    @RequiresPermissions(value = "role:edit", logical = Logical.OR)
    public JSONObject rolePermissionUpdate(@RequestBody JSONObject data) {
        try {
            Long roleId = data.getLong("roleId");
            String permissionIds = data.getString("permissionIds");
            return roleManageService.rolePermissionUpdate(roleId,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新当前角色的权限菜单 异常");
        }
    }
}
